﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithm.Struct
{
    public class AssemblyLine
    {
        public double StartTime;
        public double[] StationsTime;
        public double EndTime;
    }

    public class TwoAssemblyLineCal
    {
        private AssemblyLine _one;
        private AssemblyLine _two;

        public TwoAssemblyLineCal(AssemblyLine one, AssemblyLine two,double[][] moveTime)
        {
            this._one = one;
            this._two = two;
            this.MoveTime = moveTime;
            if (one.StationsTime.Length != _two.StationsTime.Length)
            {
                throw new Exception("Must same length");
            }
            this.StationLength =  one.StationsTime.Length;
            this.Lines = new int[2][];
            this.Lines[0] = new int[this.StationLength -1];
            this.Lines[1] = new int[this.StationLength-1];

            this.Times = new double[2][];
            this.Times[0] = new double[this.StationLength];
            this.Times[1] = new double[this.StationLength];
        }

        public int StationLength
        {
            get;
            private set;
        }

        public double[][] MoveTime
        {
            get;
            private set;
        }

        public int[][] Lines
        {
            get;
            private set;
        }

        public double[][] Times
        {
            get;
            private set;
        } 

        public double AllTime
        {
            get;
            private set;
        }

        public int EndStation
        {
            get;
            private set;
        }

        public void Cal()
        {
            this.Times[0][0] = this._one.StartTime + this._one.StationsTime[0];
            this.Times[1][0] = this._two.StartTime + this._two.StationsTime[0];

            for (int i = 1; i < this.StationLength; i++)
            {
                var line1Time =this.Times[0][i-1] + this._one.StationsTime[i];
                var line2Time = this.Times[1][i - 1] + this.MoveTime[1][i - 1] + this._one.StationsTime[i];
                if (line1Time < line2Time)
                {
                    this.Times[0][i] = line1Time;
                    this.Lines[0][i-1] = 0;
                }
                else
                {
                    this.Times[0][i] = line2Time;
                    this.Lines[0][i-1] = 1;
                }

                line1Time = this.Times[0][i - 1] + this.MoveTime[0][i - 1] + this._two.StationsTime[i];
                line2Time = this.Times[1][i-1]  + this._two.StationsTime[i];

                if (line2Time < line1Time)
                {
                    this.Times[1][i] = line2Time;
                    this.Lines[1][i - 1] = 1;
                }
                else
                {
                    this.Times[1][i] = line1Time;
                    this.Lines[1][i - 1] = 0;
                }
            }

            var allLine1Time = this.Times[0][this.StationLength - 1] + this._one.EndTime;
            var allLine2Time = this.Times[1][this.StationLength - 1] + this._two.EndTime;

            if (allLine1Time < allLine2Time)
            {
                this.AllTime = allLine1Time;
                this.EndStation = 0;
            }
            else
            {
                this.AllTime = allLine2Time;
                this.EndStation = 1;
            }

        }
    }
}
